package com.zzb.projectfactory.framework.role.controller;

import com.zzb.projectfactory.framework.common.BaseController;
import com.zzb.projectfactory.framework.role.entity.SysMenu;
import com.zzb.projectfactory.framework.role.entity.User;
import com.zzb.projectfactory.framework.role.form.SysMenuSearchForm;
import com.zzb.projectfactory.framework.role.service.SysMenuService;
import com.zzb.projectfactory.framework.common.BasePage;
import com.zzb.projectfactory.framework.common.BaseResponse;
import com.zzb.projectfactory.framework.common.ServiceException;
import com.zzb.projectfactory.framework.role.vo.SysMenuTreeVO;
import com.zzb.projectfactory.framework.role.vo.SysRoleMenuTreeVO;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
* 菜单表controller
*/
@Controller
@RequestMapping("/system/sysMenu")
public class SysMenuController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(SysMenuController.class);
    @Autowired
    private SysMenuService sysMenuService;

    /**菜单表管理首页*/
    @RequestMapping({"","/","/index"})
    public String index(){
        return "system/sysMenu/index";
    }
    /**菜单表列表json*/
    @RequestMapping("list")
    @ResponseBody
    public BasePage<SysMenu> list(SysMenuSearchForm sysMenuSearchForm){
        sysMenuSearchForm.setOrderBy("menuOrder asc");
        return sysMenuService.queryPage(sysMenuSearchForm, SysMenu.class);
    }

    /**角色菜单单页*/
    @RequestMapping("listRoleMenuUI")
    public String listRoleMenuUI(String roleId,Model model){
        SysRoleMenuTreeVO roleMenuVO = sysMenuService.getRoleMenuTreeVOByRoleId(roleId);
        model.addAttribute("roleId",roleId);
        model.addAttribute("roleMenuVO",roleMenuVO);
        return "system/sysMenu/listMenuAndOwnerUI";
    }

    /**菜单表列表json*/
    @RequestMapping("getRoleMenuTreeVO")
    @ResponseBody
    public SysRoleMenuTreeVO getRoleMenuTreeVO(String roleId){
        return sysMenuService.getRoleMenuTreeVOByRoleId(roleId);
    }


    /**菜单表列表json*/
    @RequestMapping("getTreeVo")
    @ResponseBody
    public SysMenuTreeVO getTreeVo(){
        return sysMenuService.getTreeVO();
    }

    /**菜单表表单页面*/
    @RequestMapping("form")
    public String form(String id, Model model){
        SysMenu sysMenu;
        if(StringUtils.isEmpty(id)){
            sysMenu = new SysMenu();
    } else{
        sysMenu = sysMenuService.get(id);
        if(sysMenu == null) sysMenu = new SysMenu();
    }
        model.addAttribute("sysMenu", sysMenu);
        return "system/sysMenu/form";
    }

    /**菜单表保存*/
    @RequestMapping("/save")
    @ResponseBody
    public BaseResponse<SysMenu> save(SysMenu sysMenu, HttpServletRequest request){
        try {
            User user = getCurrentUser(request);
            return sysMenuService.saveMenu(sysMenu, user);
        } catch (ServiceException e) {
            e.printStackTrace();
            return new BaseResponse<>(BaseResponse.FAIL,e.getMessage());
        }
    }

    /**菜单表删除*/
    @RequestMapping("/delete")
    @ResponseBody
    public BaseResponse delete(String id,HttpServletRequest request){
        try {
            return sysMenuService.deleteMenu(id,getCurrentUser(request));
        } catch (ServiceException e) {
            e.printStackTrace();
            return new BaseResponse(BaseResponse.FAIL,e.getMessage());
        }
    }

    /**菜单表开启*/
    @RequestMapping("/open")
    @ResponseBody
    public BaseResponse open(String id ,HttpServletRequest request){
        try {
            return sysMenuService.recovery(id,getCurrentUser(request));
        } catch (ServiceException e) {
            e.printStackTrace();
            return new BaseResponse(BaseResponse.FAIL,e.getMessage());
        }
    }

    /**菜单表屏蔽*/
    @RequestMapping("/deny")
    @ResponseBody
    public BaseResponse deny(String id ,HttpServletRequest request){
        try {
            return sysMenuService.trash(id,getCurrentUser(request));
        } catch (ServiceException e) {
            e.printStackTrace();
            return new BaseResponse(BaseResponse.FAIL,e.getMessage());
        }
    }

}
